package com.du.web.sample.util;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2021/8/6
 * @since: 1.8
 */
public class FileTypeUtil {

    private FileTypeUtil() {}

    private static ContentEnum getTypeEnum(String type) {
        if (FileType.PIC.contains(type)) {
            return ContentEnum.PIC;
        }
        if (FileType.VIDEO.contains(type)) {
            return ContentEnum.VIDEO;
        }
        if (FileType.PDF.contains(type)) {
            return ContentEnum.PDF;
        }
        if (FileType.WORD.contains(type)) {
            return ContentEnum.WORD;
        }
        return ContentEnum.OTHER;
    }
    public static ContentEnum getType(String fileName) {
        String type = FileUtil.extName(fileName).toLowerCase();
        return getTypeEnum(type);
    }
    public static ContentEnum getType(InputStream in, String fileName) {
        try {
            String type = cn.hutool.core.io.FileTypeUtil.getType(in, fileName);
            return getTypeEnum(type);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static boolean picType(File file) {
        String type = cn.hutool.core.io.FileTypeUtil.getType(file);
        return FileType.PIC.contains(type);
    }

    public static boolean picType(InputStream in) {
        String type = cn.hutool.core.io.FileTypeUtil.getType(in);
        return FileType.PIC.contains(type);
    }



    /**
     * 文件类型
     */
    public interface FileType {
        // 图片
        List<String> PIC = new ArrayList<String>() {
            {
                add("jpg");
                add("jpeg");
                add("png");
                add("gif");
                add("tif");
                add("bmp");
            }
        };

        List<String> VIDEO = new ArrayList<String>() {
            {
                add("mp4");
                add("rmvb");
                add("wav");
                add("avi");
                add("flv");
                add("wmv");
            }
        };

        List<String> PDF = new ArrayList<String>() {
            {
                add("pdf");
            }
        };

        List<String> WORD = new ArrayList<String>() {
            {
//                add("doc");
                add("docx");
            }
        };

    }


}
